package com.example.controller;


import com.example.annotation.RepeatSubmit;
import com.example.common.LogType;
import com.example.annotation.OperLog;
import com.example.dto.RoleDTO;
import com.example.service.IRoleService;
import com.example.utils.R;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 王鹤蒲
 * @since 2023-08-13
 */
@Api(tags = "角色信息API",description="提供角色信息相关的 REST API")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService iRoleService;


    @RequestMapping(value="/selectAll",method= RequestMethod.GET)
    @ResponseBody
    public R selectAll(@RequestParam int currentPage, @RequestParam int pageSize, @RequestParam(defaultValue = "") String name){
    return iRoleService.selectAll(currentPage,pageSize,name);
    }

    @RequestMapping(value="/findAll",method= RequestMethod.GET)
    @ResponseBody
    public R findAll(){
        return R.success(iRoleService.list());
    }

    @OperLog(operation = "角色管理",type = LogType.ADD)
    @RepeatSubmit(lockTime = 5)
    @RequestMapping(value="/save",method=RequestMethod.POST)
    @ResponseBody
    public R save(@RequestBody RoleDTO roleDTO) {
       return iRoleService.save(roleDTO);
    }

    @OperLog(operation = "角色管理",type = LogType.UPDATE)
    @RepeatSubmit(lockTime = 5)
    @RequestMapping(value="/edit/{id}",method=RequestMethod.POST)
    @ResponseBody
    public R edit(@PathVariable("id") Integer id, @RequestBody RoleDTO roleDTO){
        return iRoleService.edit(id,roleDTO);
    }


    @OperLog(operation = "角色管理",type = LogType.DELETE)
    @RequestMapping(value="/delete/{id}",method=RequestMethod.DELETE)
    @ResponseBody
    public R deleteById(@PathVariable("id") Integer id){
        return iRoleService.deleteById(id);
    }

    @OperLog(operation = "角色管理",type = LogType.BATCH_DELETE)
    @RequestMapping(value="/deleteBatch",method=RequestMethod.POST)
    @ResponseBody
    public R deleteByIds(@RequestBody Integer []ids){
        return iRoleService.deleteByIds(ids);
    }

    @RequestMapping(value="/{roleId}",method=RequestMethod.PUT)
    public R getRoleByRoleId(@PathVariable Integer roleId){
       return R.success(iRoleService.getRoleByRoleId(roleId));
    }

    @OperLog(operation = "角色管理",type = LogType.UPDATE)
    @RepeatSubmit(lockTime = 5)
    @RequestMapping(value="/edit/{id}",method=RequestMethod.GET)
    public R editRoleStatus(@PathVariable("id")Integer id,@RequestParam Boolean status){
        return iRoleService.editRoleStatus(id,status);
    }
}

